package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toList;

/**
 * @author: rongfeng
 * @date: 2020/12/30
 * @description
 */
public class CheckWord {
    public static void main(String[] args) {
        CheckWord checkWord = new CheckWord();
//        checkWord.start1();
//        checkWord.start2();
//        checkWord.start3();

//        checkWord.start4();

        checkWord.start5();
//        checkWord.start6();
    }

    public void start1() {
        redFile1(new File("D:\\wrf\\books\\test\\shibai.txt"));
    }

    public void start2() {
        folderMethod2("D:\\BaiduNetdiskDownload\\Peppa Pig2\\test");
    }

    public void start3() {
        folderMethod3("D:\\wrf\\qn");
    }

    public void start4() {
        folderMethod4("D:\\wrf\\qn");

        Set<String> words2 = new ReadWords().start();


        List<String> list1 = new ArrayList<>(mWords);
        List<String> list2 = new ArrayList<>(words2);

        List<String> reduce1 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
        for (String str : reduce1) {
            System.out.println(str);
        }


    }


    public void start5() {
        folderMethod5("D:\\word\\test");
    }

    public void start6() {
        folderMethod2("D:\\word\\books");
    }


    public void redFile6(File file) {

        String name = file.getName();
        int index2 = name.lastIndexOf(".");
        String name3 = name.substring(0, index2);

        String fileName1 = file.getParent().replace("books", "json") + "\\" + name3 + "_json";

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            Set<String> words = new HashSet<>();
            while ((text = bufferedReader.readLine()) != null) {
                words.add(text);
                if (!isExists(text, fileName1)) {
                    System.out.println(text);
                }
            }
//            saveFile(words, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean isExists(String word, String path) {
        File file = new File(path + "\\" + word + ".json");
        return file.exists();
    }


    public void redFile5(File file, File file2) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            Set<String> words = new HashSet<>();
            while ((text = bufferedReader.readLine()) != null) {
                words.add(text);
            }
            copyWord(words, file2);
            System.out.println("复制完成 " + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void copyWord(Set<String> words, File file2) {
        for (String word : words) {
            File file = new File("D:\\wrf\\qn3\\" + word + ".json");
            if(!file.exists()){
                file = new File("D:\\wrf\\qn\\" + word + ".json");
            }
            try {

                File file3 = new File(file2.getAbsolutePath() + "\\" + word + ".json");
                if (file3.exists()) {
                    file3.delete();
                }
//                file3.createNewFile();
                Files.copy(file.toPath(), file3.toPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public void folderMethod5(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        folderMethod5(file2.getAbsolutePath());
                    } else {


                        String name = file2.getName();
                        int index2 = name.lastIndexOf(".");
                        String name3 = name.substring(0, index2);
//

                        String fileName1 = file2.getParent();
                        int index3 = fileName1.lastIndexOf("\\");
                        String name4 = fileName1.substring(index3 + 1);

                        System.out.println(name4);
                        File file3 = new File("D:\\word\\json2\\" + name4 + "\\" + name3 + "_json");
                        if (!file3.isDirectory()) {
                            file3.mkdirs();
                        }

                        redFile5(file2, file3);


//                        System.out.println(name3);
                    }
                }
            }
        } else {
//            System.out.println("文件不存在!");
        }
    }


    public void folderMethod4(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        folderMethod3(file2.getAbsolutePath());
                    } else {
                        String name = file2.getName();
                        int index2 = name.lastIndexOf(".");
                        String name3 = name.substring(0, index2);
                        mWords.add(name3);
                    }
                }
            }
        } else {
//            System.out.println("文件不存在!");
        }
    }


    public void folderMethod3(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        folderMethod3(file2.getAbsolutePath());
                    } else {
                        redFile3(file2);
                    }
                }
            }
        } else {
//            System.out.println("文件不存在!");
        }
    }


    public void folderMethod2(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        folderMethod2(file2.getAbsolutePath());
                    } else {
                        redFile(file2);
                    }
                }
            }
        } else {
//            System.out.println("文件不存在!");
        }
    }

    Set<String> mWords = new HashSet<>();

    public void redFile(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((text = bufferedReader.readLine()) != null) {

//                if (!mWords.contains(text)) {
//                    words.add(text);
//                } else {
//                    System.out.println("删除 " + text);
//                }
                stringBuilder.append(text+"\n");
            }

            text = stringBuilder.toString().replace("1\n" +
                    "00:00:02,019 --> 00:00:05,898\n" +
                    "Www. Ebama. Net\n" +
                    "\n", "");

            saveFile(text, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void redFile3(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                if (text.contains("\\t") || text.contains("\\u003cp")) {
                    youdao.Word bean = GsonUtil.parse(text, youdao.Word.class);
                    System.out.println(bean.getWord());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void saveFile(Set<String> set, File file) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String str : set) {
            stringBuffer.append(str + "\n");
        }
        try {
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(stringBuffer.toString());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("保存完成 ");
    }


    public void saveFile(String string, File file) {
        try {
            if (file.exists()) {
                file.delete();
                file.createNewFile();
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(string);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("保存完成 ");
    }


    public void redFile1(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;

            while ((text = bufferedReader.readLine()) != null) {
                mWords.add(text);
            }
            System.out.println("sss");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void redFile4(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String text = null;
            while ((text = bufferedReader.readLine()) != null) {
                File df = new File("D:\\wrf\\qn\\" + text + ".json");
                if (df.exists()) {
                    df.delete();
                }
            }
            System.out.println("sss");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
